package com.hong.concurrent.chapter4;

import java.util.ArrayList;
import java.util.List;

/**
 * 观察者模式
 */
public class Subject {

    private List<Observer> observices = new ArrayList<>();

    private  int state;
    private Observer observer;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        if(state==this.state){
            return;
        }
        this.state = state;
        notifyAllobserver();
    }

    public void attach(Observer observer){
        this.observer = observer;
        observices.add(observer);
    }

    private void notifyAllobserver(){
        observices.stream().forEach(Observer::update);
    }

}
